package mil.af.amc.adpetracker.web.actions.struts1.User;

import mil.af.amc.adpetracker.exceptions.UserNotFoundException;
import mil.af.amc.adpetracker.service.AdpeAccountService;
import mil.af.amc.adpetracker.service.EquipmentItemService;
import mil.af.amc.adpetracker.service.UserService;
import mil.af.amc.adpetracker.service.exceptions.AdpeAccountNotFoundException;
import mil.af.amc.adpetracker.web.actions.struts1.AbstractDispatchAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;
import org.springframework.beans.factory.annotation.Required;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Dispatch Action for User related tasks
 *
 * @author Jason Ferguson
 */
public class UserDispatchAction extends AbstractDispatchAction {

    private Log log = LogFactory.getLog(UserDispatchAction.class);

    private EquipmentItemService equipmentItemService;

    @Required
    public void setItemService(EquipmentItemService itemService) {
        this.equipmentItemService = itemService;
    }

    @Required
    public void setEquipmentItemService(EquipmentItemService equipmentItemService) {
        this.equipmentItemService = equipmentItemService;
    }

    private UserService userService;

    @Required
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private AdpeAccountService adpeAccountService;

    public void setAdpeAccountService(AdpeAccountService adpeAccountService) {
        this.adpeAccountService = adpeAccountService;
    }

    public ActionForward viewCustodian(ActionMapping actionMapping,
                                       ActionForm actionForm,
                                       HttpServletRequest httpServletRequest,
                                       HttpServletResponse httpServletResponse) {


        return actionMapping.findForward("viewCustodian");

    }

    public ActionForward removeAlternateCustodian(ActionMapping actionMapping,
                                                  ActionForm actionForm,
                                                  HttpServletRequest httpServletRequest,
                                                  HttpServletResponse httpServletResponse) {

        int userId = Integer.parseInt(httpServletRequest.getParameter("userId"));
        int accountId = (Integer) httpServletRequest.getAttribute("accountId");

        try {
            adpeAccountService.removeAlternateCustodian(accountId, userId);
        } catch (AdpeAccountNotFoundException e) {

            ActionMessage actionMessage = new ActionMessage("error.account.notfound", accountId);
            saveError(httpServletRequest, actionMessage);

            return actionMapping.findForward("viewRemoveAlternateCustodian");

        } catch (UserNotFoundException e) {

            ActionMessage actionMessage = new ActionMessage("error.user.notfound", accountId);
            saveError(httpServletRequest, actionMessage);

            return actionMapping.findForward("viewRemoveAlternateCustodian");

        }

        ActionMessage actionMessage = new ActionMessage("messages.account.removealtcustodian");
        saveError(httpServletRequest, actionMessage);

        return actionMapping.getInputForward();

    }
}
